package chapter18;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class MappedIOTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		long start,end;
		String fileName="./src/test2.txt";
		DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
		start=System.nanoTime();
		while((in.read())!=-1){}
		end=System.nanoTime();
		System.out.println(end-start);
		in.close();
		FileChannel fc=new FileInputStream(new File(fileName)).getChannel();
		MappedByteBuffer mb=fc.map(MapMode.READ_ONLY, 0, fc.size());
		start=System.nanoTime();
		while(mb.hasRemaining())mb.get();
		end=System.nanoTime();
		System.out.println(end-start);
		fc.close();
		

	}

}
